package edu.nf.relaxenjoy.service.impl;

import edu.nf.relaxenjoy.common.constant.platform.PlatformConstant;
import edu.nf.relaxenjoy.common.util.ConvertUtils;
import edu.nf.relaxenjoy.common.util.RedisUtils;
import edu.nf.relaxenjoy.domain.dto.AlbumDTO;
import edu.nf.relaxenjoy.domain.entity.Album;
import edu.nf.relaxenjoy.domain.entity.AlbumRelation;
import edu.nf.relaxenjoy.domain.entity.User;
import edu.nf.relaxenjoy.domain.vo.AlbumVo;
import edu.nf.relaxenjoy.mapper.*;
import edu.nf.relaxenjoy.service.AlbumService;
import edu.nf.relaxenjoy.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhu
 * @date 2024-12-16
 */
@Service
@RequiredArgsConstructor
public class AlbumServiceImpl implements AlbumService {

    private final RedisUtils redisUtils;

    private final AlbumMapper albumMapper;

    private final UserMapper userMapper;

    private final AlbumRelationMapper albumRelationMapper;

    private final PostService postService;

    private final AgreeCollectMapper agreeCollectMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAlbum(String id, String uid) {
        //得到当前专辑下的所有图片
        List<AlbumRelation> albumRelationEntityList = albumRelationMapper.getPage(null,null, Long.valueOf(id));
        List<String> idList = albumRelationEntityList.stream().map(e -> String.valueOf(e.getMid())).collect(Collectors.toList());

        String albumStateKey = PlatformConstant.ALBUM_STATE + id;
        redisUtils.delete(albumStateKey);

        postService.deleteImgs(idList, uid);
        albumMapper.removeById(Long.valueOf(id));
    }

    @Override
    public List<AlbumVo> getAllAlbum(Long uid) {
        List<Album> albumList = albumMapper.getList(uid);
        return ConvertUtils.sourceToTarget(albumList, AlbumVo.class);
    }

    @Override
    public AlbumVo getAlbum(String id) {
        Album album = albumMapper.getById(Long.valueOf(id));
        AlbumVo albumVo = ConvertUtils.sourceToTarget(album, AlbumVo.class);
        User user = userMapper.getUserByUserId(album.getUid());
        albumVo.setUsername(user.getUsername());
        albumVo.setAvatar(user.getAvatar());
        return albumVo;
    }

    @Override
    public void saveAlbum(AlbumDTO albumDTO) {
        Album albumEntity = ConvertUtils.sourceToTarget(albumDTO, Album.class);
        albumEntity.setImgCount(0L);
        albumEntity.setCollectionCount(0L);
        albumEntity.setCreateDate(new Date());
        albumEntity.setUpdateDate(new Date());
        albumMapper.insert(albumEntity);
    }

    @Override
    public void updateAlbum(AlbumDTO albumDTO) {
        Album albumEntity = ConvertUtils.sourceToTarget(albumDTO, Album.class);
        albumEntity.setUpdateDate(new Date());
        albumMapper.updateById(albumEntity);
    }
}
